*** This code analyzes the nonfilers in the pre-populated return data

cap log close
clear
set more off
set type double
set linesize 150

log using "$logdir/analysis/analysis_nonfilers.log" , replace


****************
* Prepare data *
****************

local vlist "nonfiler soi_prim_yob cdw_* taxsim1* taxsim5*"

use `vlist' using "$statadir/soi_cdw_taxsim_2019.dta" if nonfiler==1

drop nonfiler
count

gen age = 2019 - soi_prim_yob if !missing(soi_prim_yob) & soi_prim_yob!=0

gen byte d_obligation = (taxsim1_agi>12200)
replace  d_obligation = (taxsim1_agi>13850) if age>=65

gen cdw_withholding_pos = cdw_withholding if cdw_withholding>0

gen byte d_filer_lag  = (cdw_filing_status!=0)
gen byte d_deps1      = !missing(cdw_depx)
gen byte d_deps5      = !missing(cdw_depx) | cdw_extra_deps>0
*NOTE: CDW_EXTRA_DEPS is never missing

foreach xx of numlist 1 5 {
	gen TEMP_moneyback = cdw_withholding - taxsim`xx'_fed_incm_tax
	gen refund`xx'     =  TEMP_moneyback if TEMP_moneyback>1
	gen tax_owed`xx'   = -TEMP_moneyback if TEMP_moneyback<-1
	
	if `xx'==1  gen TEMP_eitc_deps = cdw_deps18
	if `xx'==5  gen TEMP_eitc_deps = cdw_deps18 + cdw_extra_deps18
	mvencode TEMP_eitc_deps , mv(0) override

	gen byte d_refund`xx'     = !missing(refund`xx')
	gen byte d_tax_owed`xx'   = !missing(tax_owed`xx')
	gen byte d_nothing`xx'    = (d_refund`xx'==0 & d_tax_owed`xx'==0)
	gen byte d_agi_pos`xx'    = (taxsim`xx'_agi>0)
	gen byte d_eitc`xx'       = (taxsim`xx'_eitc>0)
	gen byte d_nokid_eitc`xx' = (d_eitc`xx'==1 & TEMP_eitc_deps==0)
	gen byte d_cheitc`xx'     = (d_eitc`xx'==1 & TEMP_eitc_deps>0)
	gen byte d_cheitc_ctc`xx' = (d_cheitc`xx'==1 | taxsim`xx'_ctc>0 | taxsim`xx'_addl_ctc>0)

	foreach yy of numlist 100 250 500 1000 2000 3000 {
		gen byte d_refund`xx'_`yy'   = (d_refund`xx'==1   & refund`xx'>=`yy')
		gen byte d_tax_owed`xx'_`yy' = (d_tax_owed`xx'==1 & tax_owed`xx'>=`yy')
	}

	gen taxsim`xx'_agi_if_refund = taxsim`xx'_agi if d_refund`xx'==1

	drop TEMP*
}


tab d_eitc1
tab d_cheitc1     if d_eitc1==1
tab d_nokid_eitc1 if d_eitc1==1

tab d_eitc5
tab d_cheitc5     if d_eitc5==1
tab d_nokid_eitc5 if d_eitc5==1

count if d_cheitc_ctc1==1
count if d_cheitc_ctc5==1
count if d_cheitc_ctc1==1 & d_obligation==0
count if d_cheitc_ctc5==1 & d_obligation==0



**********************
* Stats in the paper *
**********************

** what is the total tax liability on pre-populated returns, among non-filers?
preserve
	collapse (sum) taxsim1_fed_incm_tax cdw_withholding , fast
	replace taxsim1_fed_incm_tax = round(1000*taxsim1_fed_incm_tax/1e9)
	replace cdw_withholding      = round(1000*cdw_withholding/1e9)
	list
restore

** what fraction of non-filers apparantly are due a refund?
tab d_refund1

** what is total refund amount apparently due to nonfilers?
preserve
	keep if !missing(refund1)
	collapse (sum) refund1 , fast
	replace refund1 = round(refund1*1000)
	list
restore

** what is total tax apparently owed by nonfilers?
preserve
	keep if !missing(tax_owed1)
	collapse (sum) tax_owed1 , fast
	replace tax_owed1 = round(tax_owed1*1000)
	list
restore

** what fraction of non-filers had dependents in T-1 ?
tab d_deps1

** of non-filers NOT required to file, how many get child EITC/CTC on their prepop return?
count if d_obligation==0 & d_cheitc_ctc1==1
count if d_obligation==0 & d_cheitc_ctc5==1



*********
* Table *
*********

tab d_obligation

tab cdw_sex_nonfiler                                                                            if d_obligation==0
tab cdw_sex_nonfiler                                                                            if d_obligation==0 & cdw_sex_nonfiler!="U"
sum d_filer_lag d_deps1 d_nokid_eitc1 d_cheitc_ctc1 d_cheitc_ctc5 d_refund1 d_tax_owed1         if d_obligation==0
sum age taxsim1_agi taxsim1_agi_if_refund cdw_withholding cdw_withholding_pos refund1 tax_owed1 if d_obligation==0 , d

tab cdw_sex_nonfiler                                                                            if d_obligation==1
tab cdw_sex_nonfiler                                                                            if d_obligation==1 & cdw_sex_nonfiler!="U"
sum d_filer_lag d_deps1 d_nokid_eitc1 d_cheitc_ctc1 d_cheitc_ctc5 d_refund1 d_tax_owed1         if d_obligation==1
sum age taxsim1_agi taxsim1_agi_if_refund cdw_withholding cdw_withholding_pos refund1 tax_owed1 if d_obligation==1 , d


cap log close
